import {ref} from 'vue'

export function useCalendar(options = {}) {
    // 获取明天的日期
    const tomorrow = new Date(Date.now() + 24 * 60 * 60 * 1000)

    const showCalendar = ref(false)
    // 设置默认值为明天
    const selectedDate = ref(options.defaultDate || tomorrow)

    // 日期范围设置
    const minDate = new Date()
    const maxDate = new Date(new Date().setMonth(new Date().getMonth() + 3))

    // 格式化日期
    const formatDate = (date) => {
        if (!date) return ''
        const d = new Date(date)
        return `${d.getMonth() + 1}-${d.getDate()}`
    }

    // 获取星期
    const getWeekDay = (date) => {
        if (!date) return ''
        const weekDays = "日一二三四五六"
        return "周" + weekDays[new Date(date).getDay()]
    }

    // 日期选择确认
    const onConfirmDate = (date) => {
        showCalendar.value = false
        selectedDate.value = date
        if (options.onSelect) {
            options.onSelect(date)
        }
    }

    // 打开日历
    const openCalendar = () => {
        showCalendar.value = true
    }

    return {
        showCalendar,
        selectedDate,
        minDate,
        maxDate,
        formatDate,
        getWeekDay,
        onConfirmDate,
        openCalendar
    }
}